library(foreign)
library(ggeffects)

setwd("/Users/joelsievert/Dropbox/Research/Legislative Responsiveness/National Road/data/PRQ Replication")

h17 <- read.csv("h17_transfer.csv")
h20 <- read.csv("h20_transfer.csv")
h23 <- read.csv("h23_transfer.csv")

#17th
m1 <- glm(rc ~ dem_rep + slave_p  + dist + distrib_cmte, family = binomial, data = h17)

p1 <- ggpredict(m1, terms = "dist[1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4, 3.6, 3.8, 4]", condition = c(dem_rep = 1, slave_p = 0, distrib_cmte = 0))
p2 <- ggpredict(m1, terms = "slave_p[0, 0.025, 0.05, 0.075, 0.10, 0.125, 0.15, 0.175, 0.20, 0.225, 0.25]", condition = c(dem_rep = 1, dist = 2.8, slave_p = 0, distrib_cmte = 0))


#23rd

m5 <-glm(rc ~ jackson  + dist+ slave_p  + distrib_cmte , family = binomial, data = h23)

p3 <- ggpredict(m5, terms = "dist[1.2, 1.4, 1.6, 1.8, 2,2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4, 3.6]", condition = c(jackson = 1, slave_p = 0, distrib_cmte = 0))
p4 <- ggpredict(m5, terms = "slave_p[0, 0.025, 0.05, 0.075, 0.10, 0.125, 0.15, 0.175, 0.20, 0.225, 0.25]", condition = c(jackson = 1, dist = 2.4, distrib_cmte = 0))



par(mfrow = c(2,2))


#######################
#######################
###DISTANCE###
######################
######################

#17th Congress
par(mar = c(5, 4, 3, 2), bg = "white")
plot(p1$x, p1$predicted, ylim = c(0, 1), xlim = c(1.8, 4), type = 'n', xlab='', ylab='', axes= FALSE )

abline(v = seq(1.8, 4, .1), col = 'grey85', lwd = 80)
abline(v = seq(1.8, 4, .2), h = seq(0, 1, 0.1), col = 'white')
lines(p1$x, p1$predicted, lwd = 3)
lines(p1$x, p1$conf.low, lwd = 3, lty = 2)
lines(p1$x, p1$conf.high, lwd = 3, lty = 2)

#code puts axis text and an angle
axis(side = 2, at = seq(0, 1, 0.1), labels = seq(0, 1, 0.1), las = 2, lwd = 0 ) 
axis(side = 1, at = seq(1.8, 4, 0.2), lwd = 0)

mtext("Distance to Nat'l Road (100s miles)", side = 1, line = 2.75)
mtext("Predicted Probability of Support", side = 2, line = 2.5)

mtext("Taylor Amendment", side = 3, line = 0.5, cex = 1, font = 2)


#Maysville Road
par(mar = c(5, 4, 3, 2), bg = "white")
plot(p3$x, p3$predicted, ylim = c(0, 1), xlim = c(1.2, 3.6), type = 'n', xlab='', ylab='', axes= FALSE )

abline(v = seq(1.2, 3.6, .1), col = 'grey85', lwd = 80)
abline(v = seq(1.2, 3.6, .2), h = seq(0, 1, 0.1), col = 'white')
lines(p3$x, p3$predicted, lwd = 3)
lines(p3$x, p3$conf.low, lwd = 3, lty = 2)
lines(p3$x, p3$conf.high, lwd = 3, lty = 2)

#code puts axis text and an angle
axis(side = 2, at = seq(0, 1, 0.1), labels = seq(0, 1, 0.1), las = 2, lwd = 0 ) 
axis(side = 1, at = seq(1.2, 3.6, 0.2), lwd = 0)

mtext("Distance to Nat'l Road (100s miles)", side = 1, line = 2.75)
#mtext("Predicted Probability", side = 2, line = 2.75)

mtext("McKennan Amendment", side = 3, line = 0.5, cex = 1, font = 2)

#######################
#######################
###SLAVE POPULATION###
######################
######################

#17th Congress
par(mar = c(5, 4, 3, 2), bg = "white")
plot(p2$x, p2$predicted, ylim = c(0, 1), xlim = c(0, 0.25), type = 'n', xlab='', ylab='', axes= FALSE )

abline(v = seq(0, 0.25, .01), col = 'grey85', lwd = 80)
abline(v = seq(0, 0.25, .025), h = seq(0, 1, 0.1), col = 'white')
lines(p2$x, p2$predicted, lwd = 3)
lines(p2$x, p2$conf.low, lwd = 3, lty = 2)
lines(p2$x, p2$conf.high, lwd = 3, lty = 2)

#code puts axis text and an angle
axis(side = 2, at = seq(0, 1, 0.1), labels = seq(0, 1, 0.1), las = 2, lwd = 0 ) 
axis(side = 1, at = seq(0, 0.25, 0.05), lwd = 0)

mtext("Slave Population (%)", side = 1, line = 2.75)
mtext("Predicted Probability of Support", side = 2, line = 2.75)

mtext("Taylor Amendment", side = 3, line = 0.5, cex = 1, font = 2)


#23rd Congress
par(mar = c(5, 4, 3, 2), bg = "white")
plot(p4$x, p4$predicted, ylim = c(0, 1), xlim = c(0, 0.25), type = 'n', xlab='', ylab='', axes= FALSE )

abline(v = seq(0, 0.25, .01), col = 'grey85', lwd = 80)
abline(v = seq(0, 0.25, .025), h = seq(0, 1, 0.1), col = 'white')
lines(p4$x, p4$predicted, lwd = 3)
lines(p4$x, p4$conf.low, lwd = 3, lty = 2)
lines(p4$x, p4$conf.high, lwd = 3, lty = 2)

#code puts axis text and an angle
axis(side = 2, at = seq(0, 1, 0.1), labels = seq(0, 1, 0.1), las = 2, lwd = 0 ) 
axis(side = 1, at = seq(0, 0.25, 0.05), lwd = 0)

mtext("Slave Population (%)", side = 1, line = 2.75)
#mtext("Predicted Probability", side = 2, line = 2.75)

mtext("McKennan Amendment", side = 3, line = 0.5, cex = 1, font = 2)

